<?php
require_once "funct.xml2array.php";
function ovfNewsJavascript() {
	global $objSess;
	$sJS = '';
	$sJS .= "var news = new Array();\n";
	
	$aRssUrl = $objSess->aRssUrl;
	$aArticoli = ovfGetRss($aRssUrl, 20);
	$nn=0;
	foreach($aArticoli as $kX => $aArtX)	{
		if (date("Ymd",$aArtX['dataOra']) == date("Ymd")) {
			$ora = date("H:i",$aArtX['dataOra']);
		} else {
			$ora = date("d/m",$aArtX['dataOra']);
		}
		$titolo = (strlen($aArtX['titolo'])<60)?$aArtX['titolo']:(substr($aArtX['titolo'],0,58)."...");
      $titolo = preg_replace("/[^a-zA-Z0-9]/"," ",$titolo);
      if (trim($titolo)!='') {
   		$news = "<div style='padding:1px'>$ora&nbsp-&nbsp"
   			."<a class='cmd1' target='_blank' href='".trim($aArtX['link'])."'>"
   			.htmlentities($titolo)."</a></div>";
   		$sJS .= "news[$nn] = \"$news\";\n";
         //dbg_($titolo);
      }
		$nn++;
	}
	$sJS .= "//alert(news.length);\n";
	$sJS .= "var nn = ".rand(0, $nn-1).";\n";
	$sJS .= "function scriviNews() {\n";
	$sJS .= "	if (news.length == 0) return true;\n";
	$titNews = "<div style='padding:1px;text-align:center;color:white;background-color:#a0a0a0;'"
		." onClick='apriNews();'>NEWS</div>";
	$sJS .= "	sHtml = \"$titNews\";\n";
	$sJS .= "	if (news.length <= 3) {\n";
	$sJS .= "	   for(kk=0;kk<news.length;kk++) {\n";
	$sJS .= "			sHtml = sHtml + news[kk];";
	$sJS .= "	   }\n";
	$sJS .= "	   oNewsBox = document.getElementById(\"newsBox\");\n";
	$sJS .= "	   oNewsBox.innerHTML = sHtml;\n";
	$sJS .= "		return true;\n";
	$sJS .= "	} \n";
	$sJS .= "	for(kk=0;kk<3;kk++) {\n";
	$sJS .= "	   if ((nn+kk)>=news.length) nn=0;\n";
	$sJS .= "		sHtml = sHtml + news[nn+kk];\n";
	$sJS .= "	}\n";
	$sJS .= "	//alert(sHtml);\n";
	$sJS .= "	oNewsBox = document.getElementById(\"newsBox\");\n";
	$sJS .= "	oNewsBox.innerHTML = sHtml;\n";
	$sJS .= "	setTimeout('scriviNews()',5000);\n";
	$sJS .= "	nn = nn+1;\n";
	$sJS .= "	if (nn >= news.length) nn = 0;\n";
	$sJS .= "}\n";
	$sJS .= "function apriNews() {\n";
	$sJS .= "	window.open('".OVF_URL_ROOT."/app/_com/newsView.php?xsn=".XSN."','ovfNews'\n";
	$sJS .= "	   ,'top=200,left=200,height=600,width=700,scrollbars=yes,resizable=yes');\n";
	$sJS .= "}\n";
	return $sJS;
}

function ovfGetRss($aRssUrl, $max=20) {
	$aArticoli = array();
	$aArtX = array();
	$dataOraCancFile = date('YmdHi',time()-3600*24); //data/ora cancellazione file rss	
	foreach($aRssUrl as $kX => $urlX) {
		$dataOraValid = date('YmdHi',time()-rand(3500,3700)); // data/ora validita' file rss
		$prefX = md5($urlX);
		$llPrefX = strlen($prefX);
		$fileName = '';
		if ($dh=opendir(OVF_FS_RSS_DIR)) {
			while($fnX = readdir($dh)) {
				if (substr($fnX,0,$llPrefX)!=$prefX) continue;
				if (substr($fnX,$llPrefX,12) < $dataOraCancFile) {
					unlink(OVF_FS_RSS_DIR."/".$fnX);
					continue;
				}
				if (substr($fnX,$llPrefX,12) < $dataOraValid) {
					continue;
				}
				if ($fnX > $fileName) $fileName = $fnX;
			}
		}
		if ($fileName=='') {
			$sRssXml = ovfScriviRss($urlX);
		} else {
			$sRssXml = file_get_contents(OVF_FS_RSS_DIR."/".$fileName);
		}
		//dbg_("sRssXml=".$sRssXml."/");
		if (!$sRssXml) continue;
		$aXml = xml2array($sRssXml);
		$fonte = utf8_decode(trim(ovfVx($aXml,'aXml','rss',0,'channel',0,'title',0)));
		$fonteLink = utf8_decode(trim(ovfVx($aXml,'aXml','rss',0,'channel',0,'link',0)));
		$aItems = ovfVx($aXml,'aXml','rss',0,'channel',0,'item');
		if (is_array($aItems)) foreach ($aItems as $kX =>$aX) {
		   if (trim(utf8_decode(ovfVx($aX,'title',0)))=='') continue;
			$j = count($aArtX);
			$dataOra = strtotime(ovfVx($aX,'pubDate',0));
			$sPref = date("YmdHis",$dataOra);
			$aArtX[$sPref.$j]['fileName'] = $fileName;
			$aArtX[$sPref.$j]['fonte'] = $fonte;
			$aArtX[$sPref.$j]['fonte_link'] = $fonteLink;
			$aArtX[$sPref.$j]['link'] = utf8_decode(ovfVx($aX,'link',0));
			$aArtX[$sPref.$j]['kArt'] = $kX;
			$aArtX[$sPref.$j]['titolo'] = trim(utf8_decode(ovfVx($aX,'title',0)));
			$aArtX[$sPref.$j]['testo'] = trim(utf8_decode(html_entity_decode(ovfVx($aX,'description',0))));
			$aArtX[$sPref.$j]['dataOra'] = $dataOra;
		}
	}
	krsort($aArtX,SORT_STRING);
	foreach($aArtX as $kX => $aX) {
		//dbg_(date("Y-m-d H:i:s ",$aX['dataOra']).$aX['titolo']);
		$aArticoli[] = $aX;
		if (count($aArticoli)>=$max) break;
	}
	return $aArticoli;
}

function ovfScriviRss($urlX) {
	$prefX = md5($urlX);
	$ctx = stream_context_create(array('http'=>array('timeout'=>3))); 
	$sRssXml = file_get_contents($urlX,0,$ctx);
	$fileName = $prefX.date('YmdHi');
	$pathRssXml = OVF_FS_RSS_DIR."/".$fileName.".xml";
	file_put_contents($pathRssXml,$sRssXml);
	return $sRssXml;
}

